<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function sendMsg(type,payload){
        localStorage.setItem('@@'+type,JSON.stringify({
            payload,
            temp:Date.now()
        }))
    }
    function listenMsg(handler){
        const storageHandler = (e)=>{
            const data = JSON.parse(e.newValue)
            handler(e.key,substring(2),data.payload);
        }
        // 监听本地存储的变化
        window.addEventListener('storage', storageHandler);
        return ()=>{
            // 取消 监听本地存储的变化
            window.removeEventListener('storage', storageHandler);
        }
    }
    listenMsg((type,payload)=>{
        console.log(type,payload)
    });
</script>

</html>